package com.dogstalks.network;

import java.io.IOException;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.dogstalks.data.*;
import android.util.Log;

public class ResponseXMLParser {
	private static final String DEBUG = "dogstalks_paraing";

	public static String parsingResultXML(XmlPullParser dogInfoInsertXmlParser) {
		String resultMessage = null;
		try {
			while (true) {
				int eventType = dogInfoInsertXmlParser.nextTag();
				if (eventType == XmlPullParser.START_TAG) {
					String tagName = dogInfoInsertXmlParser.getName();
					if (tagName.equals("result_value"))
						resultMessage = dogInfoInsertXmlParser.nextText()
								.trim();
				} else if (eventType == XmlPullParser.END_TAG) {
					String tagName = dogInfoInsertXmlParser.getName();
					if (tagName.equals("result"))
						break;
				}
			}
		} catch (XmlPullParserException xppe) {
			Log.e(DEBUG, "parsingResultXML() error : ", xppe);
		} catch (IOException ioe) {
			Log.e(DEBUG, "parsingResultXML() error : ", ioe);
		}
		Log.d(DEBUG, "result_value ok : " + resultMessage);
		return resultMessage;
	}

	public static ArrayList<DogInfo> parsingSelectDogInfoXML(
			XmlPullParser dogInfoSelectXmlParser) {
		ArrayList<DogInfo> dogInfo = null;
		int i = 0;
		if (dogInfoSelectXmlParser != null) {
			dogInfo = new ArrayList<DogInfo>();
			while (true) {
				int eventType;
				i++;
				try {
					eventType = dogInfoSelectXmlParser.nextTag();
					if (eventType == XmlPullParser.START_TAG) {
						String tag = dogInfoSelectXmlParser.getName();
						if ("dog".equals(tag)) {
							Log.d(DEBUG, "dogInfo START_TAG");
							DogInfo temp = parsingDogListXML(dogInfoSelectXmlParser);
							dogInfo.add(temp);
						}
					} else if (eventType == XmlPullParser.END_TAG) {
						String tag = dogInfoSelectXmlParser.getName();
						if ("dogInfo".equals(tag)) {
							Log.d(DEBUG, "dogInfo END_TAG");
							break;
						} else
							continue;
					}
				} catch (XmlPullParserException xppe) {
					Log.e(DEBUG, "parsingSelectDogInfoXML() error : ", xppe);
				} catch (IOException ioe) {
					Log.e(DEBUG, "parsingSelectDogInfoXML() error : ", ioe);
				}
			}
		}
		return dogInfo;
	}

	public static DogInfo parsingDogListXML(XmlPullParser dogInfoSelectXmlParser) {
		DogInfo dogInfo = null;
		if (dogInfoSelectXmlParser != null) {
			dogInfo = new DogInfo();
			while (true) {
				int eventType;
				try {
					eventType = dogInfoSelectXmlParser.nextTag();
					if (eventType == XmlPullParser.START_TAG) {
						String tag = dogInfoSelectXmlParser.getName();
						if ("email".equals(tag)) {
							dogInfo.id = dogInfoSelectXmlParser.nextText()
									.trim();
						} else if ("name".equals(tag)) {
							dogInfo.name = dogInfoSelectXmlParser.nextText()
									.trim();
							Log.d(DEBUG, dogInfo.name);
						} else if ("species".equals(tag)) {
							dogInfo.species = dogInfoSelectXmlParser.nextText()
									.trim();
							Log.d(DEBUG, dogInfo.species);
						} else if ("profile_url".equals(tag)) {
							dogInfo.profileUrl = dogInfoSelectXmlParser
									.nextText().trim();
							Log.d(DEBUG, dogInfo.profileUrl + "");
						} else if ("latitude".equals(tag)) {
							dogInfo.latitude = Integer
									.parseInt(dogInfoSelectXmlParser.nextText()
											.trim());
						} else if ("longitude".equals(tag)) {
							dogInfo.longitude = Integer
									.parseInt(dogInfoSelectXmlParser.nextText()
											.trim());
							dogInfo.setGeoPoint();
						} else {
							parsePass(dogInfoSelectXmlParser);
							Log.d(DEBUG, "DogList parsing no");
						}
					} else if (eventType == XmlPullParser.END_TAG) {
						String tag = dogInfoSelectXmlParser.getName();
						if ("dog".equals(tag))
							break;
						else
							continue;
					}
				} catch (XmlPullParserException xppe) {
					Log.e(DEBUG, "parsingSelectDogInfoListXML() error : ", xppe);
				} catch (IOException ioe) {
					Log.e(DEBUG, "parsingSelectDogInfoListXML() error : ", ioe);
				}
			}
		}
		return dogInfo;
	}

	public static DogInfo parsingSelectOneDogInfoXML(
			XmlPullParser dogInfoSelectXmlParser) {
		DogInfo dogInfo = null;
		int i = 0;
		if (dogInfoSelectXmlParser != null) {
			dogInfo = new DogInfo();
			while (true) {
				int eventType;
				i++;
				try {
					eventType = dogInfoSelectXmlParser.nextTag();
					if (eventType == XmlPullParser.START_TAG) {
						
						String tag = dogInfoSelectXmlParser.getName();
						if ("dog".equals(tag)) {
							Log.d(DEBUG, "dogInfo START_TAG");
							dogInfo = new DogInfo();
							dogInfo = parsingOneDogListXML(dogInfoSelectXmlParser);
						}
					} else if (eventType == XmlPullParser.END_TAG) {
						String tag = dogInfoSelectXmlParser.getName();
						if ("dogInfo".equals(tag)) {
							Log.d(DEBUG, "dogInfo END_TAG");
							break;
						} else
							continue;
					}
				} catch (XmlPullParserException xppe) {
					Log.e(DEBUG, "parsingSelectDogInfoXML() error : ", xppe);
				} catch (IOException ioe) {
					Log.e(DEBUG, "parsingSelectDogInfoXML() error : ", ioe);
				}
			}
		}
		return dogInfo;
	}

	public static DogInfo parsingOneDogListXML(
			XmlPullParser dogInfoSelectXmlParser) {
		DogInfo dogInfo = null;
		if (dogInfoSelectXmlParser != null) {
			while (true) {
				int eventType;
				try {
					eventType = dogInfoSelectXmlParser.nextTag();
					if (eventType == XmlPullParser.START_TAG) {
						String tag = dogInfoSelectXmlParser.getName();
						if ("email".equals(tag)) {
							dogInfo = new DogInfo();
							dogInfo.id = dogInfoSelectXmlParser.nextText()
									.trim();
							Log.d(DEBUG, " ID parsing : " + dogInfo.id);
						} else if ("password".equals(tag)) {
							dogInfo.password = dogInfoSelectXmlParser
									.nextText().trim();
						} else if ("name".equals(tag)) {
							dogInfo.name = dogInfoSelectXmlParser.nextText()
									.trim();
						} else if ("species".equals(tag)) {
							dogInfo.species = dogInfoSelectXmlParser.nextText()
									.trim();
						} else if ("age".equals(tag)) {
							dogInfo.age = dogInfoSelectXmlParser.nextText()
									.trim();
						} else if ("gender".equals(tag)) {
							dogInfo.gender = dogInfoSelectXmlParser.nextText()
									.trim();
						} else if ("twitter".equals(tag)) {
							dogInfo.twitter = dogInfoSelectXmlParser.nextText()
									.trim();
						} else if ("facebook".equals(tag)) {
							dogInfo.facebook = dogInfoSelectXmlParser
									.nextText().trim();
						} else if ("profile_url".equals(tag)) {
							dogInfo.profileUrl = dogInfoSelectXmlParser
									.nextText().trim();
						} else if ("latitude".equals(tag)) {
							dogInfo.latitude = Integer
									.parseInt(dogInfoSelectXmlParser.nextText()
											.trim());
						} else if ("longitude".equals(tag)) {
							dogInfo.longitude = Integer
									.parseInt(dogInfoSelectXmlParser.nextText()
											.trim());
							dogInfo.setGeoPoint();
						} else {
							parsePass(dogInfoSelectXmlParser);
							Log.d(DEBUG, "DogList parsing no");
						}
					} else if (eventType == XmlPullParser.END_TAG) {
						String tag = dogInfoSelectXmlParser.getName();
						if ("dog".equals(tag))
							break;
						else
							continue;
					}
				} catch (XmlPullParserException xppe) {
					Log.e(DEBUG, "parsingSelectDogInfoListXML() error : ", xppe);
				} catch (IOException ioe) {
					Log.e(DEBUG, "parsingSelectDogInfoListXML() error : ", ioe);
				}
			}
		}
		return dogInfo;
	}

	public static ArrayList<Tracking> parsingSelectTrackingXML(
			XmlPullParser trackingSelectXmlParser) {
		ArrayList<Tracking> tracking = null;
		if (trackingSelectXmlParser != null) {
			tracking = new ArrayList<Tracking>();
			while (true) {
				int eventType;
				try {
					eventType = trackingSelectXmlParser.nextTag();
					if (eventType == XmlPullParser.START_TAG) {
						String tag = trackingSelectXmlParser.getName();
						if ("tracking".equals(tag)) {
							Log.d(DEBUG, "trackingInfo START_TAG");
							Tracking temp = parsingTrackingListXML(trackingSelectXmlParser);
							tracking.add(temp);
						} else {
							Log.d(DEBUG, "parsingSelectTrackingXML selectTracking no");
							parsePass(trackingSelectXmlParser);
						}
					} else if (eventType == XmlPullParser.END_TAG) {
						String tag = trackingSelectXmlParser.getName();
						if ("trackingInfo".equals(tag)) {
							Log.d(DEBUG, "trackingInfo END_TAG");
							break;
						} else
							continue;
					}
				} catch (XmlPullParserException xppe) {
					Log.e(DEBUG, "parsingSelectTrackingXML() error : ", xppe);
				} catch (IOException ioe) {
					Log.e(DEBUG, "parsingSelectTrackingXML() error : ", ioe);
				}
			}
		}
		return tracking;
	}

	public static Tracking parsingTrackingListXML(
			XmlPullParser trackingSelectXmlParser) {
		Tracking tracking = null;
		if (trackingSelectXmlParser != null) {
			while (true) {
				int eventType;
				try {
					eventType = trackingSelectXmlParser.nextTag();
					if (eventType == XmlPullParser.START_TAG) {
						String tag = trackingSelectXmlParser.getName();
						if ("count".equals(tag)) {
							tracking = new Tracking();
							tracking.count = Integer
									.valueOf(trackingSelectXmlParser.nextText()
											.trim());
						} else if ("latitude".equals(tag)) {
							tracking.latitude = Integer
									.valueOf(trackingSelectXmlParser.nextText()
											.trim());
						} else if ("longitude".equals(tag)) {
							tracking.longitude = Integer
									.valueOf(trackingSelectXmlParser.nextText()
											.trim());
						} else {
							parsePass(trackingSelectXmlParser);
							Log.d(DEBUG, "Tracking parsing no");
						}
					} else if (eventType == XmlPullParser.END_TAG) {
						String tag = trackingSelectXmlParser.getName();
						if ("tracking".equals(tag))
							break;
						else
							continue;
					}
				} catch (XmlPullParserException xppe) {
					Log.e(DEBUG, "parsingTrackingListXML() error : ", xppe);
				} catch (IOException ioe) {
					Log.e(DEBUG, "parsingTrackingListXML() error : ", ioe);
				}
			}
		}
		return tracking;
	}

	public static ArrayList<String> parsingSelectGeoPictureXML(
			XmlPullParser pictrueSelectXmlParser) {
		ArrayList<String> picture_url = null;
		int i = 0;
		if (pictrueSelectXmlParser != null) {
			picture_url = new ArrayList<String>();
			Log.d(DEBUG, "parsingSelectGeoPictureXML selectDogInfo()");
			while (true) {
				int eventType;
				i++;
				try {
					eventType = pictrueSelectXmlParser.nextTag();
					if (eventType == XmlPullParser.START_TAG) {
						String tag = pictrueSelectXmlParser.getName();
						if ("picture_url".equals(tag)) {
							Log.d(DEBUG, "geoPicture parsing: " + tag);
							String temp = pictrueSelectXmlParser.nextText()
									.trim();
							picture_url.add(temp);
						}
					} else if (eventType == XmlPullParser.END_TAG) {
						String tag = pictrueSelectXmlParser.getName();
						if ("geoPicture".equals(tag)) {
							Log.d(DEBUG, "geoPicture END_TAG");
							break;
						} else
							continue;
					}
				} catch (XmlPullParserException xppe) {
					Log.e(DEBUG, "parsingSelectGeoPictureXML() error : ", xppe);
				} catch (IOException ioe) {
					Log.e(DEBUG, "parsingSelectGeoPictureXML() error : ", ioe);
				}
			}
		}
		return picture_url;
	}

	public static void parsePass(XmlPullParser xpp)
			throws XmlPullParserException, IOException {
		int type = xpp.next();
		if (type == XmlPullParser.START_TAG) {
			xpp.getName();
		} else if (type == XmlPullParser.TEXT) {
			xpp.getText();
		}
	}

}
